/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.event.MouseEvent;
import java.util.*;

/**
 *
 *
 */
abstract public class GUILayout extends GUIComponent {

    Vector guiComponents = new Vector();
    protected int visible = 1;
    protected int isSubMenu = 0;
    protected int open = 0;

    /**
     * Add a component to this layout
     */
    public void add(GUIComponent toAdd) {
        toAdd.setParent(parent);
        guiComponents.add(toAdd);
        doLayout();
    }

    /**
     * Clear all of the components in this layout
     */
    public void flush() {

        guiComponents = new Vector();

    }

    abstract protected void doLayout();

    /**
     * Handle mouse presses.
     * @return integer indicating whether the mouse event affected the layout.
     */
    public int mousePressed(MouseEvent e) {
        /*
        GUIComponent temp;
        int mouseX = g.mouseX;
        int mouseY = g.mouseY;

        //check if they are clicking the layout
        if (visible == 1 && mouseX < locX+cWidth && mouseX > locX && mouseY > locY && mouseY < locY ){
        parent.focusedComp = this;
        return 1;

        }
         */
        return 0;

    }

    /**
     * Handle mouse clicks.
     * @return integer indicating whether the mouse event affected the layout.
     */
    public int mouseClicked(int mouseX, int mouseY) {

        GUIComponent temp;

        //check if they are clicking the layout
        if (visible == 1 && mouseX < locX + cWidth && mouseX > locX && mouseY < locY && mouseY > locY) {
            parent.focusedComp = this;
            return 1;
        }

        return 0;

    }

    /**
     * Handle mouse clicks.
     * @return integer indicating whether the mouse event affected the layout.
     */
    public int mouseMoved(int mouseX, int mouseY) {

        GUIComponent temp;

        int padding = 10;

        if (visible == 1 && mouseX < locX + cWidth + padding && mouseX > locX - padding &&
                mouseY <= locY + padding && mouseY > locY-cHeight - padding) {
            over = 1;
        } else {
            over = 0;
        }

        for (int i = 0; i < guiComponents.size(); i++) {
            temp = (GUIComponent) guiComponents.elementAt(i);
            if (visible == 1 && temp.mouseMoved(mouseX, mouseY) == 1) {
                over = 1;
            }
        }

        if (over == 0 && open == 0 && isSubMenu == 1) {
            setVisible(0);

        }


        return over;

    }

    public void setVisible(int visible) {
        this.visible = visible;
    }

    public void setIsSubMenu(int isSubMenu) {
        this.isSubMenu = isSubMenu;
    }

    public void setPosition(int x, int y) {
        //why doesn't super(x,y) work here?
        locX = x;
        locY = y;
        doLayout();
    }

    public void toggleVisible() {
        visible = 1 - visible;
    }
}
